package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.CarriageInfoPublishEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyCarriageManageModel extends BaseViewModel {
    public SelfRepertory repertory;
    private int page = 1;
    public MutableLiveData<ListResponse<List<CarriageInfoPublishEntity>>> liveData = new MutableLiveData();
    public MutableLiveData<ListResponse<List<CarriageInfoPublishEntity>>> liveDataMore = new MutableLiveData();

    public MyCarriageManageModel(@NonNull @NotNull Application application) {
        super(application);
    }

    //承运列表
    public void myCarrierList(Context context, boolean lodMore, int messageStatus) {
        page = lodMore ? page + 1 : 1;  //？？
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("status", true);
        map.put("page", page);
        map.put("limit", 10);
        if (messageStatus != 0) {
            map.put("messageStatus", messageStatus);
        }
        NewAPIManager.getInstance().getLogisticsAPI().myCarrierHistoryList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CarriageInfoPublishEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CarriageInfoPublishEntity>>>> call, Response<BaseEntity<ListResponse<List<CarriageInfoPublishEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!lodMore) {
                        liveData.postValue(response.body().getData());
                    } else {
                        liveDataMore.postValue(response.body().getData());
                    }
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CarriageInfoPublishEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
